package com.uzhie.web.service.listener;

import com.uzhie.jt808.JT808Constant;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.BytesMessage;
import javax.jms.Destination;

/**
 * Created by zhenghao on 2017/7/4.
 */
public class MQSendServiceBak implements MQSendService{

    private Logger logger = Logger.getLogger(this.getClass());

    private JmsTemplate jmsTemplate;

    private ActiveMQDestination[] activeMQDestinations;

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void setMqDestination(ActiveMQDestination mqDestination) {
        activeMQDestinations = new ActiveMQDestination[5];
        ActiveMQDestination[] ads = mqDestination.getCompositeDestinations();
        for (ActiveMQDestination ad : ads) {
            if (ad.getPhysicalName().equals("position")) activeMQDestinations[0] = ad;
            else if (ad.getPhysicalName().equals("origin")) activeMQDestinations[1] = ad;
            else if (ad.getPhysicalName().equals("picture")) activeMQDestinations[2] = ad;
            else if (ad.getPhysicalName().equals("passenger")) activeMQDestinations[3] = ad;
            else if (ad.getPhysicalName().equals("ftp_file")) activeMQDestinations[4] = ad;
        }
    }

    public MQSendServiceBak() {
        logger.info("MQSendServiceBak 被加载,当前实例 " + this);
    }

    /**
     * 根据不同类型 向不同的队列发送信息
     *
     * @param queue
     * @param ID
     * @param body
     */
    @Override
    public void sendMessage(Integer queue, final String ID, final byte[] body) {
        switch (queue) {
            case JT808Constant.MSG_ID.DEV_REQ_MEDIA_DATA:
                Destination destination2 = activeMQDestinations[2];
                jmsTemplate.send(destination2, session -> {
                    BytesMessage message = session.createBytesMessage();
                    message.setIntProperty("type", JT808Constant.MSG_ID.DEV_REQ_MEDIA_DATA);
                    message.setStringProperty("simNumber", ID);
                    message.writeBytes(body);
                    return message;
                });
                logger.info(Thread.currentThread().getName() + " 向队列" + destination2.toString());
                logger.info(Thread.currentThread().getName() + " 发送图片保存消息 sim卡号-------->" + ID);
                logger.info(Thread.currentThread().getName() + " 消息体内容 -------->" + body);
                break;
            case JT808Constant.MSG_ID.DEV_REQ_POSITION:
                Destination destination = activeMQDestinations[0];
                jmsTemplate.send(destination, session -> {
                    BytesMessage message = session.createBytesMessage();
                    message.setIntProperty("type", JT808Constant.MSG_ID.DEV_REQ_POSITION);
                    message.setStringProperty("simNumber", ID);
                    message.writeBytes(body);
                    return message;
                });
                logger.info(Thread.currentThread().getName() + " 向队列" + destination.toString());
                logger.info(Thread.currentThread().getName() + "发送定位消息 sim卡号-------->" + ID);
                logger.info(Thread.currentThread().getName() + " 消息体内容 -------->" + body);
                break;
            case JT808Constant.MSG_ID.PLA_REQ_PLAYBACK_CATALOG:
                Destination destination1 = activeMQDestinations[1];
                jmsTemplate.send(destination1, session -> {
                    BytesMessage message = session.createBytesMessage();
                    message.setIntProperty("type", JT808Constant.MSG_ID.PLA_REQ_PLAYBACK_CATALOG);
                    message.setStringProperty("simNumber", ID);
                    message.writeBytes(body);
                    return message;
                });
                logger.info(Thread.currentThread().getName() + " 向队列" + destination1.toString());
                logger.info(Thread.currentThread().getName() + " 发送远程目录查询消息 sim卡号-------->" + ID);
                logger.info(Thread.currentThread().getName() + " 消息体内容 -------->" + body);
                break;
            case JT808Constant.MSG_ID.DEV_REQ_PASSENGER:
                Destination destination3 = activeMQDestinations[3];
                jmsTemplate.send(destination3, session -> {
                    BytesMessage message = session.createBytesMessage();
                    message.setIntProperty("type", JT808Constant.MSG_ID.DEV_REQ_PASSENGER);
                    message.setStringProperty("simNumber", ID);
                    message.writeBytes(body);
                    return message;
                });
                logger.info(Thread.currentThread().getName() + " 向队列" + destination3.toString());
                logger.info(Thread.currentThread().getName() + " 客流量消息 sim卡号-------->" + ID);
                logger.info(Thread.currentThread().getName() + " 消息体内容 -------->" + body);
                break;
            case JT808Constant.MSG_ID.DEV_REQ_FILE_UPLOAD_COMPLETE:
                Destination destination4 = activeMQDestinations[4];
                jmsTemplate.send(destination4, session -> {
                    BytesMessage message = session.createBytesMessage();
                    message.setIntProperty("type", JT808Constant.MSG_ID.DEV_REQ_FILE_UPLOAD_COMPLETE);
                    message.setStringProperty("simNumber", ID);
                    message.writeBytes(body);
                    return message;
                });
                logger.info(Thread.currentThread().getName() + " 向队列" + destination4.toString());
                logger.info(Thread.currentThread().getName() + " FTP文件上传完成 sim卡号-------->" + ID);
                logger.info(Thread.currentThread().getName() + " 消息体内容 -------->" + body);
                break;
            default:
                break;
        }

    }

}
